package com.qf.smartplatform.event.listener;/*
*王浩政
*whz
2022/9/13
11:30
*小米商城
*/



import com.qf.smartplatform.event.LoginFailEvent;
import com.qf.smartplatform.event.LoginSuccessEvent;
import com.qf.smartplatform.pojo.SysLogininfor;
import com.qf.smartplatform.pojo.SysUserOnline;
import com.qf.smartplatform.service.LoginLogService;
import com.qf.smartplatform.service.OnLineUserService;
import com.qf.smartplatform.service.UserService;
import com.qf.smartplatform.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class LoginEventListener {

//    private UserService userService;
//
//    @Autowired
//    public void setUserService(UserService userService){
//        this.userService=userService;
//    }

    private TaskExecutor taskExecutor;

    @Autowired
    public void setTaskExecutor(TaskExecutor taskExecutor){
        this.taskExecutor=taskExecutor;
    }

    private OnLineUserService onLineUserService;

    @Autowired
    public void setOnLineUserService(OnLineUserService onLineUserService){
        this.onLineUserService=onLineUserService;
    }

   private LoginLogService loginLogService;

//    @Autowired
    public void setLoginLogService(LoginLogService loginLogService) {
        this.loginLogService = loginLogService;
    }
    //    public void setLoginLogService(LoginLogService loginLogService) {
//        this.loginLogService = loginLogService;
//    }

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService){
        this.userService=userService;
    }

   // @Async//异步执行
    @EventListener
    public void onEvent(LoginSuccessEvent event) throws InterruptedException {
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName());
//        userService.updateLoginData(event.getUId(),event.getIp());
        //userService.updateLoginData(event.getuId(),event.getIp());
        //获取到request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //根据浏览器来获取user-agent获取用户的浏览器，操作系统
        Map<String, String> osAndBrowserInfo = RequestUtil.getOsAndBrowserInfo(request);
        String sessionId = request.getSession().getId();
        String ip = request.getRemoteHost();
        //异步执行
        CompletableFuture.runAsync(()->{

            //更新登录的时间和ip
            userService.updateLoginData(event.getuId(),ip);
        },taskExecutor);
//        //获取系统和浏览器的信息
//        String os = osAndBrowserInfo.get("os");
//        String browser = osAndBrowserInfo.get("browser");
//        System.out.println(os);
//        System.out.println(browser);

        CompletableFuture.runAsync(()->{
//           //获取系统和浏览器的信息
//       String os = osAndBrowserInfo.get("os");
//       String browser = osAndBrowserInfo.get("browser");
//       System.out.println(os);
//       System.out.println(browser);
//       //根据ip获取地址
//       String locationByIp = RequestUtil.getLocationByIp(ip);
//            String locationByIp="局域网地址";
//       System.out.println(locationByIp);
//       //TODO 将数据插入到数据库中，注意，理论上一个用户名应该只有一行数据

//            SysUserOnline sysUserOnline = new SysUserOnline();
//
//            sysUserOnline.setSessionId(sessionId);
//            sysUserOnline.setBrowser();
//            sysUserOnline.setExpireTime();
//            sysUserOnline.setIpaddr();
//            sysUserOnline.setLastAccessTime();
//            sysUserOnline.setLoginName(even);
//            sysUserOnline.setLoginLocation();
//            sysUserOnline.setOs();
//            sysUserOnline.setStartTimestamp();
//            sysUserOnline.setStatus();
//
//            SysUserOnline sysUserOnline = new SysUserOnline();
//            sysUserOnline.setSessionId(sessionId);
//            sysUserOnline.setLoginName(event.getUserName());
//            sysUserOnline.setIpaddr(ip);
//            //设置登录地址,用上面的登录日志中的数据来设置
//            sysUserOnline.setLoginLocation(locationByIp);
//            sysUserOnline.setBrowser(browser);
//            sysUserOnline.setOs(os);
//            sysUserOnline.setStatus("1");
//            sysUserOnline.setStartTimestamp(new Date());
//            sysUserOnline.setLastAccessTime(new Date());
//            sysUserOnline.setExpireTime(30L);
//            onLineUserService.addOnLinrData(sysUserOnline);

//            //登录日志的数据
//            SysLogininfor sysLogininfor = new SysLogininfor();
//            sysLogininfor.setBrowser(browser);
//            sysLogininfor.setIpaddr(ip);
//            sysLogininfor.setLoginLocation(locationByIp);
//            sysLogininfor.setLoginName(event.getUserName());
//            sysLogininfor.setLoginTime(new Date());
//            sysLogininfor.setMsg("登陆成功");
//            sysLogininfor.setOs(os);
//            sysLogininfor.setStatus("1");
//
//
//            //同时插入到登录日志表中
//            loginLogService.addLog(sysLogininfor);
            SysLogininfor sysLogininfor = addLog(osAndBrowserInfo, ip, event.getUserName());
            sysLogininfor.setStatus("1");
            sysLogininfor.setMsg("登录还就内个陈昆！");
            // 同时插入到登录日志表中
          loginLogService.addLog(sysLogininfor);
        //获取系统和浏览器的信息
        String os = osAndBrowserInfo.get("os");
        String browser = osAndBrowserInfo.get("browser");
        System.out.println(os);
        System.out.println(browser);

            //       //根据ip获取地址
////       String locationByIp = RequestUtil.getLocationByIp(ip);
//            String locationByIp="局域网地址";
//       System.out.println(locationByIp);
       //TODO 将数据插入到数据库中，注意，理论上一个用户名应该只有一行数据
          //            SysUserOnline sysUserOnline = new SysUserOnline();

            SysUserOnline sysUserOnline = new SysUserOnline();
            sysUserOnline.setSessionId(sessionId);
            sysUserOnline.setLoginName(event.getUserName());
            sysUserOnline.setIpaddr(ip);
            //设置登录地址,用上面的登录日志中的数据来设置
            sysUserOnline.setLoginLocation(sysLogininfor.getLoginLocation());
            sysUserOnline.setBrowser(browser);
            sysUserOnline.setOs(os);
            sysUserOnline.setStatus("1");
            sysUserOnline.setStartTimestamp(new Date());
            sysUserOnline.setLastAccessTime(new Date());
            sysUserOnline.setExpireTime(30L);
            onLineUserService.addOnLinrData(sysUserOnline);
        },taskExecutor);


    }
//    @EventListener
//    public void onLoginFail(LoginFailEvent event) {
//        //获取到request
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        //根据请求头user-agent获取用户的浏览器,操作系统
//        Map<String, String> osAndBrowserInfo = RequestUtil.getOsAndBrowserInfo(request);
//        String ip = request.getRemoteHost();
//        //在异步中添加数据
//        CompletableFuture.runAsync(() -> {
//            SysLogininfor sysLogininfor = addLog(osAndBrowserInfo, ip, event.getUserName());
//            sysLogininfor.setStatus("0");
//            sysLogininfor.setMsg("登录失败");
//            //同时插入到登录日志表中
//            loginLogService.addLog(sysLogininfor);
//        },taskExecutor);
//    }

    @EventListener
public void onLoginFail(LoginFailEvent event){
        //获取到request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //根据请求头(浏览器来获取)user-agent获取用户的浏览器，操作系统
        Map<String, String> osAndBrowserInfo = RequestUtil.getOsAndBrowserInfo(request);
        String ip = request.getRemoteHost();
        //在异步中添加数据
        CompletableFuture.runAsync(()->{
            SysLogininfor sysLogininfor = addLog(osAndBrowserInfo, ip, event.getUserName());
            sysLogininfor.setStatus("0");
            sysLogininfor.setMsg("登录失败啦！！！");
           // 同时插入到登录日志表中
           loginLogService.addLog(sysLogininfor);
        },taskExecutor);


}

/*
* 将数据封装到登录日志对象中
* */



        public SysLogininfor addLog(Map<String, String> osAndBrowserInfo,String ip,String userName){
//设置登录地址,用上面的登录日志中的数据来设置

            //           //获取系统和浏览器的信息
//       String os = osAndBrowserInfo.get("os");
//       String browser = osAndBrowserInfo.get("browser");
//       System.out.println(os);
//       System.out.println(browser);

            //同时插入到登录日志表中
            String os = osAndBrowserInfo.get("os");
            String browser = osAndBrowserInfo.get("browser");
            System.out.println(os);
            System.out.println(browser);
            //根据ip获取地址
            //String locationByIp=RequesUtil.getLocationByIp(ip);
            String locationByIp="局域网地址";

            //登录日志的数据
            SysLogininfor sysLogininfor1 = new SysLogininfor();
            sysLogininfor1.setBrowser(browser);
            sysLogininfor1.setIpaddr(ip);
            sysLogininfor1.setLoginLocation(locationByIp);
            sysLogininfor1.setLoginName(userName);
            sysLogininfor1.setLoginTime(new Date());
//            sysLogininfor1.setMsg("登陆成功");
            sysLogininfor1.setOs(os);
//            sysLogininfor1.setStatus("1");
            return sysLogininfor1;

//            //同时插入到登录日志表中
//           loginLogService.addLog(sysLogininfor1);

        }

}

















